package io.renren.modules.headmaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.headmaster.service.MasterStudentService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.teacher.service.TeacherStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wks
 * @date 2022/2/10 15:57
 */
@RestController
@RequestMapping("/headmaster/student")
public class MasterStudentController {
    @Autowired
    MasterStudentService masterStudentService;
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        params.put("classId", ShiroUtils.getUserEntity().getClassId());
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = masterStudentService.selectStudentList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }

    /**
     * 保存用户
     */
    @SysLog("班主任添加学生")
    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user) {
        user.setClassId(ShiroUtils.getUserEntity().getClassId());
        ValidatorUtils.validateEntity(user, AddGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterSaveTeacher(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("班主任修改学生")
    @PostMapping("/update")
    //@RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        if (user.getClassId() == null) {
            user.setClassId(ShiroUtils.getUserEntity().getClassId());
        }
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterUpdateStudent(user);

        return R.ok();
    }
}
